๐Ÿ“ฆ james7132 / GTCourseWork

๐Ÿ“„ autograder.py ยท 276 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276import NeuralNetUtil
import NeuralNet
import traceback
import sys
import getopt
from os import listdir
import cPickle

def sameList(list1,list2,floatDif):
    if len(list1)!=len(list2):
        return False
    for index in xrange(len(list1)):
        if isinstance(list1[index],float):
            if abs(list1[index]-list2[index])>floatDif:
                return False
        elif list1[index]!=list2[index]:
            return False
    return True

def copyWeights(nnet1, nnet2):
    for l in xrange(len(nnet1.layers)):
        for n in xrange(len(nnet1.layers[l])):
            nnet1.layers[l][n].weights = nnet2.layers[l][n].weights
            nnet1.layers[l][n].inSize = nnet2.layers[l][n].inSize

def q1Test(testFile):
    if 'sigmoid' in testFile.name and 'Activation' not in testFile.name:
        value = float(testFile.readline().strip())
        sPercep = NeuralNet.Perceptron()
        solution = sPercep.sigmoid(value)
    else:
        testFuncName = testFile.readline().strip()
        getData = getattr(NeuralNetUtil, testFuncName)
        examples, tests = getData()
        testRangeStart = testFile.readline().strip()
        testRangeEnd = testFile.readline().strip()
        testRangeStart = 0 if testRangeStart=='None' else int(testRangeStart)
        testRangeEnd = len(examples) if testRangeEnd=='None' else int(testRangeEnd)
        examples = examples[testRangeStart:testRangeEnd]
        
        if 'feedforward' in testFile.name:
            sNet = NeuralNet.NeuralNet([16,24,10])
            
            file = open('test_cases/nnet')
            net = cPickle.load(file)
            copyWeights(sNet,net)
            
            solution = []
            for example in examples:
                solution.append(sNet.feedForward(example[0]))
        elif 'Activation' in testFile.name:
            file = open('test_cases/percep')
            percep = cPickle.load(file)
            
            sPercep = NeuralNet.Perceptron(inSize = percep.inSize, weights = percep.weights)
            solution = []
            for example in examples:
                solution.append(sPercep.sigmoidActivation(example[0]))
    return solution

def q2Test(testFile):
    if 'sigmoidDeriv' in testFile.name:
        solution = []
        percep = NeuralNet.Perceptron()
        for line in testFile:
            val = float(line)
            solution = percep.sigmoidDeriv(val)
    else:
        testFuncName = testFile.readline().strip()
        getData = getattr(NeuralNetUtil, testFuncName)
        examples, tests = getData()
        testRangeStart = testFile.readline().strip()
        testRangeEnd = testFile.readline().strip()
        testRangeStart = 0 if testRangeStart=='None' else int(testRangeStart)
        testRangeEnd = len(examples) if testRangeEnd=='None' else int(testRangeEnd)
        examples = examples[testRangeStart:testRangeEnd]
        
        file = open('test_cases/percep')
        percep = cPickle.load(file)

        sPercep = NeuralNet.Perceptron(inSize = percep.inSize-1, weights = percep.weights)
        if 'update' in testFile.name:
            solution = []
            for example in examples:
                solution.append(sPercep.updateWeights(example[0],0.1,0.67))
        elif 'sigmoid' in testFile.name:
            solution = []
            for example in examples:
                solution.append(sPercep.sigmoidActivationDeriv(example[0]))
    return solution

def q3Test(testFile,module=NeuralNet):
    testFuncName = testFile.readline().strip()
    getData = getattr(NeuralNetUtil, testFuncName)
    examples, tests = getData()
    testRangeStart = testFile.readline().strip()
    testRangeEnd = testFile.readline().strip()
    testRangeStart = 0 if testRangeStart=='None' else int(testRangeStart)
    testRangeEnd = len(examples) if testRangeEnd=='None' else int(testRangeEnd)
    examples = examples[testRangeStart:testRangeEnd]
    sNet = NeuralNet.NeuralNet([16,24,10])
    
    file = open('test_cases/nnet')
    net = cPickle.load(file)
    copyWeights(sNet,net)
    
    solution = sNet.backPropLearning(examples,0.1)
    return solution[1]

def q4Test(testFile,module=NeuralNet):
    testFuncName = testFile.readline().strip()
    getData = getattr(NeuralNetUtil, testFuncName)
    examples = getData()
    testRangeStart = testFile.readline().strip()
    testRangeEnd = testFile.readline().strip()
    testRangeStart = 0 if testRangeStart=='None' else int(testRangeStart)
    testRangeEnd = len(examples) if testRangeEnd=='None' else int(testRangeEnd)
    examples = (examples[0][testRangeStart:testRangeEnd],examples[1][testRangeStart:testRangeEnd])
    
    alph = float(testFile.readline())
    weight = float(testFile.readline())
    
    file = open('test_cases/nnet')
    net = cPickle.load(file)
    
    sNet = NeuralNet.NeuralNet([16,24,10])
    copyWeights(sNet,net)
    
    solution = NeuralNet.buildNeuralNet(examples, alpha=alph, weightChangeThreshold = weight,startNNet = sNet)
    return solution[1]
  
def getFile(name, q):
    return open('test_cases/'+q+'/'+name)

def runTests(q,points=2):
    total = 0
    possible = 0
    testFunc = globals()[q+'Test']
    correct=False
    print 'Running %s tests\n'%q
    for fileName in [name for name in listdir('test_cases/'+q) if 'test' in name]:
        possible+=1
        print 'Running test %s'%fileName
        
        try:
            solFile = open('test_cases/'+q+'/'+fileName.replace('.test','.solution'))
            solution = solFile.read().split(';')
        except Exception,e:
            print 'No solution file found'
            continue
        
        try:
            result = testFunc(getFile(fileName,q))
            exec(solution[0].strip())
        except Exception,e:
            print 'You broke something:'
            print traceback.format_exc()
            continue
        if correct:
            total+=1
            print 'Correct answer!'
        else:
            print 'Your answer is incorrect'
            print 'Your answer: %s'%str(result)
            print 'Correct answer: %s\n'%str(solution[1].strip())
    
        print '--------------------------------------------------------------------'
    if total==possible:
        print 'All tests passed - score %d/%d'%(points,points)
        print 'Done\n____________________________________________________________________\n'
        return (points,points)
    else:
        print 'Not all tests passed - score 0/%d'%points
        print 'Done\n____________________________________________________________________\n'
        return (0,points)

def makeSolutionsFiles(q, floatDif = 0.000001):
    testFunc = globals()[q+'Test']
    for fileName in [name for name in listdir('test_cases/'+q) if 'test' in name]:
        print 'Creating solution file for test %s'%fileName
        solution = testFunc(getFile(fileName,q))
        solFile = open('test_cases/'+q+'/'+fileName.replace('.test','.solution'),'w')
        if isinstance(solution,float):
            solFile.write('correct=abs(result-%f)<%f;\n%f'%(solution,floatDif,solution))    
        elif isinstance(solution,list):
            solFile.write('correct=sameList(result,%s,%f);\n%s'%(str(solution),floatDif,str(solution)))
        else:
            solFile.write('correct=str(result)=="""%s""";\n%s'%(str(solution),str(solution)))    
        print 'Solution is \n%s\n'%str(solution)

def runTest(test,questions):
    try:
        solFile = open(test.replace('.test','.solution'))
        solution = solFile.read().split(';')
        q = None
        for question in questions:
            if question in test:
                q = question
        testFunc = globals()[q+'Test']
        result = testFunc(open(test))
        correct = False
        exec(solution[0].strip())
        if correct:
            print 'Correct answer!'
        else:
            print 'Your answer is incorrect'
            print 'Your answer: %s'%str(result)
            print 'Correct answer: %s\n'%str(solution[1].strip())
    except Exception,e:
        print 'No solution file found for test %s, or you broke something'%test

def q1Tests():
    return runTests('q1',2)

def q2Tests():
    return runTests('q2',2)

def q3Tests():
    return runTests('q3',4)

def q4Tests():
    return runTests('q4',4)

def makePickledObjects():
    data = NeuralNetUtil.buildExamplesFromPenData() 
    net = NeuralNet.buildNeuralNet(data, weightChangeThreshold=0.00075,hiddenLayerList = [24])[0]
    print net
    f = open('test_cases/nnet','w')
    cPickle.dump(net,f)
    f.close()
    
    f = open('test_cases/percep','w')
    cPickle.dump(net.layers[0][1],f)
    f.close()
help_string = 'Usage: autograder.py [options]\n\
\
Run public tests on student code\n\
\
Options:\n\
  -h, --help            show this help message and exit\n\
  -t RUNTEST, --test=RUNTEST\n\
                        Run one particular test.  Relative to test root.\n\
  -q GRADEQUESTION, --question=GRADEQUESTION\n\
                        Grade one particular question.'

def main():
    args = sys.argv
    questions = ['q1','q2','q3','q4']
    if len(args)==1:
        sumTotal= 0
        sumPossible = 0
        for q in questions:
            func = globals()[q+'Tests']
            total, possible = func()
            sumTotal+=total
            sumPossible+=possible 
        print '\nAutograder finished. Final score %d/%d'%(sumTotal,sumPossible)
    else:
        opts, args = getopt.getopt(args[1:],"q:t:h",["q=","test=","help"])
        for opt, arg in opts:
            if opt=='-q' or opt=='--q':
                if arg in questions:
                    func = globals()[arg+'Tests']
                    func()
            elif opt=='-t' or opt=='--test':
                runTest(arg,questions)
            elif opt=='-h' or opt=='--telp':
                print help_string
                break

if __name__=='__main__':
    #questions = ['q1','q2','q3','q4']
    #for question in questions:
    #    makeSolutionsFiles(question)
    main()